<?php
class IndexControl extends CommonControl
{
	/**
	 * 文章列表页
	 */
	public function actionIndex($page = 1)
	{
		$postModel = postsModel::model();
		$pageObj = new dpPage(array(
			'base_url'   => 'index/index',
			'total_rows' => count($postModel->findsByAttribute(array('id'))),
			'per_page'   => 5,
			'num_links'  => 2,
			'page_container_tag_style' => 'page',
			'num_tag_style'	=> 'num_page',
		));

		$postList = $postModel->limit($pageObj->getSqlLimit())->sort_time()->finds();
		$this->assign('post_list', $postList);
		$this->assign('pages',     $pageObj->display());
		$this->assign('page',	   $page);
		unset($postModel, $pageObj, $postList);
		$this->render();
	}

	/**
	 * 文章详细页
	 */
	public function actionShow($id = 0)
	{
		if((int)$id == 0) D::error('骚年，不要做坏事！');
		$id    = (int)$id;
		$info  = postsModel::model()->findByPk($id);
		$token = new dpEncrypt();
		empty($info) && D::error('#'.$id.' 文章不存在');
		$reply = replyModel::model()->sort_time()->finds('posts_id=:id', array(':id'=>$id));
		$bread = $this->getBreadCrumbs(array('首页'=>'index', $info['title']=>''));
		postsModel::model()->views = $info['views'] + 1;
		postsModel::model()->updateByPk($id);
		$var   = array(
			'info'  => $info,
			'reply' => $reply,
			'token_code'   => $token->encode('add_comment_auth','DuolamPHP'),
			'bread_crumbs' => $bread
		);
		unset($id, $info, $reply, $bread);
		$this->render('show', $var);
	}

	public function actionAddComment()
	{
		if(isset($_POST['token']))
		{
			$token = new dpEncrypt();
			if($token->decode($_POST['token'], 'DuolamPHP') != 'add_comment_auth') 
				$this->ajaxReturn(array('code'=>1, 'msg'=>'非法操作'));
			else
			{
				$id 	 = (int)$_POST['posts_id'];
				$name    = dpText::removeXss(dpText::printHtml($_POST['name']));
				$email   = dpText::removeXss(dpText::printHtml($_POST['email']));
				$content = dpText::removeXss(dpText::printHtml($_POST['content']));
				$code    = $_POST['code'];
				empty($id) && $this->ajaxReturn(array('code'=>1, 'msg'=>'文章不存在，无法评论'));
				empty($name) && $this->ajaxReturn(array('code'=>2, 'name'=>'name'));
				empty($content) && $this->ajaxReturn(array('code'=>2, 'name'=>'content'));
				empty($code) && $this->ajaxReturn(array('code'=>2, 'name'=>'code'));
				dpImage::checkVerify('code', $code) or $this->ajaxReturn(array('code'=>1, 'msg'=>'验证码不正确'));
				$data = array(
					'posts_id' => $id,
					'name'     => $name,
					'email'    => $email,
					'content'  => $content,
					'reply_time' => TIME
				);
				if(replyModel::model()->insert($data))
				{
					$info  = postsModel::model()->findByPk($id);
					postsModel::model()->reply_number = $info['reply_number'] + 1;
					postsModel::model()->updateByPk($id);
				}
				$html = $this->_getAjaxComment($id);
				if(!$html) $this->ajaxReturn(array('code'=>1, '评论失败'));
				$this->ajaxReturn(array('code'=>0, 'html'=>$html));
			}
		}
		else
			$this->ajaxReturn(array('code'=>1, 'msg'=>'非法操作'));
	}

	private function _getAjaxComment($posts_id)
	{
		if(empty($posts_id)) return false;
		$model = replyModel::model();
		$id    = $model->id;
		$name  = $model->name;
		$email = $model->email;
		$content = $model->content;
		$reply_time = $model->reply_time;
		$count = count($model->finds('posts_id='.$posts_id)); 
		if(!empty($email)) $email = '邮箱：'.$email.' ';
		$date = date('Y-m-d', $reply_time);
return <<<EOF
<div class="reply_content new_reply_element" style="display:none">
	&nbsp;<a name="F{$id}">#{$count}</a>&nbsp;{$name}：
	<div class="re_c">
		<div>{$email}评论时间：{$date}</div>
		&nbsp;{$content}
	</div>
</div>
EOF;
	}


}

?>